package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.InstitutionType;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.InstitutionTypeService;
import com.wty.cmims.mapper.InstitutionTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【institution_type】的数据库操作Service实现
* @createDate 2024-12-12 17:28:05
*/
@Service
public class InstitutionTypeServiceImpl extends ServiceImpl<InstitutionTypeMapper, InstitutionType>
    implements InstitutionTypeService{
    @Autowired
    private InstitutionTypeMapper institutionTypeMapper;
    @Override
    public ResponseResult<Map<String, Object>> institutionTypeFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> institutionTypes = institutionTypeMapper.findInstitutionType(paramsMap);
        if (institutionTypes != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗机构类型信息成功");
            responseResult.setDatas(institutionTypes);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗机构类型信息失败");
        }
        return responseResult;
    }

    @Override
    @Transactional
    public ResponseResult<Map<String, Object>> institutionTypesFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);

        List<Map<String, Object>> institutionTypes = institutionTypeMapper.findInstitutionTypes(paramsMap);
        Integer total = institutionTypeMapper.findInstitutionTypesNum(paramsMap);
        if (institutionTypes != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询科室信息成功");
            responseResult.setDatas(institutionTypes);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询科室信息失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> institutionTypeUpdate(InstitutionType institutionType) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        boolean result = institutionTypeMapper.updateById(institutionType) > 0;
        if (result) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("修改科室信息成功");
        } else {
            responseResult.setCode(Code.ERROR);
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> institutionTypeAdd(InstitutionType institutionType) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        int rows = institutionTypeMapper.insert(institutionType);
        if (rows > 0) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("添加科室信息成功");
        } else {
            responseResult.setCode(Code.ERROR);
        }
        return responseResult;
    }
}




